package com.dingding.service.mall.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.UserCommodityOrder;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.mall.dto.UserCommodityOrderDTO;
import com.dingding.facade.mall.sql.UserCommodityOrderSQL;
import com.dingding.service.mall.dao.IUserCommodityOrderDao;

/**
 * @Description:用户商城订单表dao
 * @author fengshuonan
 * @date 2016年3月23日 下午4:27:37
 */
@Component("userCommodityOrderDao")
public class UserCommodityOrderDaoImpl extends BaseDaoImpl<UserCommodityOrder> implements IUserCommodityOrderDao {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description:生成商城订单
	 * @param userId
	 *            用户id
	 * @param itemId
	 *            商品id
	 * @param price
	 *            商品价格
	 * @author fengshuonan
	 * @return true 生成成功 false 生成失败
	 */
	public boolean createShopOrder(String userId, String itemId, double price) {

		UserCommodityOrder userCommodityOrder = new UserCommodityOrder();
		userCommodityOrder.setLoginId(userId);
		userCommodityOrder.setCreateTime(new Date());
		userCommodityOrder.setCommodityId(itemId); // 商品的id
		userCommodityOrder.setCommodityOrderId(ToolUtil.getUid()); // 订单的主键id
		userCommodityOrder.setCommodityPrice(price);
		userCommodityOrder.setConsignor(userId);
		userCommodityOrder.setOrderStatus(2);// 1:待发货 2:已发货

		try {
			super.save(userCommodityOrder);
		} catch (Exception e) {
			log.error("生成商品订单失败！UserCommodityOrderDaoImpl：createShopOrder（）", e);
			return false;
		}

		return true;
	}

	/**
	 * @ClassName: IUserCommodityOrderListDao
	 * @Description:获取用户商城订单信息列表
	 * @author zhouxuan
	 * @date 2016年3月25日 上午10:42:23
	 */
	public List<UserCommodityOrderDTO> getUserCommodityOrderList(String userId) {

		List<Object[]> obj = null;
		List<UserCommodityOrderDTO> userCommodityOrderDTO = new ArrayList<UserCommodityOrderDTO>();

		try {
			obj = super.listSqlObj(UserCommodityOrderSQL.GET_USER_COMMODITY_ORDER_SQL, userId);
		} catch (Exception e) {
			log.error("获取用户积分记录信息失败！UserCommodityOrderListDaoImpl:getUserCommodityOrderList", e);
		}

		serResUserCommodityOrderDTO(obj, userCommodityOrderDTO);
		return userCommodityOrderDTO;
	}

	/**
	 * @Title: serResUserCommodityOrderDTO
	 * @Description:SQL结果转换bean
	 */
	private void serResUserCommodityOrderDTO(List<Object[]> obj, List<UserCommodityOrderDTO> userCommodityOrderDTO) {

		if (obj != null) {
			for (Object[] temp : obj) {
				if (temp != null && temp.length > 0) {
					UserCommodityOrderDTO userCommodityOrderDTOtemp = new UserCommodityOrderDTO();
					userCommodityOrderDTOtemp.setCommodityName(temp[0].toString()); // 商品名称
					userCommodityOrderDTOtemp.setCommodityPrice(new Double(temp[1].toString())); // 商品价格
					userCommodityOrderDTOtemp.setCommodityId(new Integer(temp[2].toString())); // 商品id
					userCommodityOrderDTOtemp.setCommodityBackGroundPic(temp[3].toString()); // 商品背景图片
					userCommodityOrderDTOtemp.setCommodityThumbnailName(temp[4].toString());// 商品缩略图
					userCommodityOrderDTOtemp.setCreateTime((Date)temp[5]);//商品下单时间
					userCommodityOrderDTO.add(userCommodityOrderDTOtemp);
				}

			}
		}

	}
}
